import re
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from subfactory.models import Document
from subfactory.tools.tools import DocumentFormatter
from subfactory.tools.tools import DocumentLine
from subfactory.tools.tools import Traduction
from subfactory.tools.tools import DocumentContainer

class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)

class DocumentFormatterTest(TestCase):
	def test_format_xb(self):
		initialContent = r'\xef\xbb\xbf'+"""1
			00:02:04,738 --> 00:02:07,865
			People always ask me 
			if I know Tyler Durden
			"""
		expected = """<document lang="EN" target_lang="FR" title="Fight Club">
			<line number="1">
			<initial>
			People always ask me
			if I know Tyler Durden
			</initial>
			<traduction>

			</traduction>
			<start>00:02:04,738</start>
			<end>00:02:07,865</end>
			</line>
			</document>
			"""
		document=Document(content=initialContent, lang="EN", target_lang="FR", title="Fight Club")
		formatter = DocumentFormatter()
		formatter.format(document)
		content = re.sub("[\r\n|\t]","",document.content)
		expected_content = re.sub("[\r\n|\t]","", expected)
		self.assertEquals(content, expected_content)

	def test_format(self):
		initialContent = """
			1
			00:02:04,738 --> 00:02:07,865
			People always ask me 
			if I know Tyler Durden
			"""
		expected = """<document lang="EN" target_lang="FR" title="Fight Club">
			<line number="1">
			<initial>
			People always ask me
			if I know Tyler Durden
			</initial>
			<traduction>

			</traduction>
			<start>00:02:04,738</start>
			<end>00:02:07,865</end>
			</line>
			</document>
			"""
		document=Document(content=initialContent, lang="EN", target_lang="FR", title="Fight Club")
		formatter = DocumentFormatter()
		formatter.format(document)
		content = re.sub("[\r\n|\t]","",document.content)
		expected_content = re.sub("[\r\n|\t]","", expected)
		self.assertEquals(content, expected_content)


	def test_format_twolines(self):
		initialContent = """
			1
			00:02:04,738 --> 00:02:07,865
			People always ask me 
			if I know Tyler Durden

			2
			00:02:08,962 --> 00:02:09,986
			Three minutes.
			"""
		expected = """<document lang="EN" target_lang="FR" title="Fight Club">
			<line number="1">
			<initial>
			People always ask me
			if I know Tyler Durden
			</initial>
			<traduction>
			
			</traduction>
			<start>00:02:04,738</start>
			<end>00:02:07,865</end>
			</line>
			<line number="2">
			<initial>
			Three minutes.
			</initial>
			<traduction>
			
			</traduction>
			<start>00:02:08,962</start>
			<end>00:02:09,986</end>
			</line>
			</document>
			"""
		document=Document(content=initialContent, lang="EN", target_lang="FR", title="Fight Club")
		formatter = DocumentFormatter()
		formatter.format(document)
		content = re.sub("[\r\n|\t]","",document.content)
		expected_content = re.sub("[\r\n|\t]","", expected)
		self.assertEquals(content, expected_content)

class DocumentLineTest(TestCase):
	def test_init(self):
		line = DocumentLine(12, "EN","my content", "00:01", "00:02")
		self.assertEquals(line.number, 12)
		self.assertEquals(line.content, "my content")
		self.assertEquals(line.lang, "EN")
		self.assertEquals(line.start, "00:01")
		self.assertEquals(line.end, "00:02")
		self.assertEquals(1, len(line.traductions))
		self.assertEquals("FR", line.traductions[0].lang)
		self.assertEquals("", line.traductions[0].content)
		
class DocumentContainerTest(TestCase):
	def test_parse(self):
		xml =  """<document lang="EN" target_lang="FR" title="Fight Club">
			<line number="1">
			<initial>
			People always ask me
			if I know Tyler Durden
			</initial>
			<traduction>
			Les gens me demandent toujours
			si je connais Tyler Durden
			</traduction>
			<start>00:02:04,738</start>
			<end>00:02:07,865</end>
			</line>
			<line number="2">
			<initial>
			Three minutes.
			</initial>
			<traduction>
			Trois minutes.	
			</traduction>
			<start>00:02:08,962</start>
			<end>00:02:09,986</end>
			</line>
			</document>
			"""
		container = DocumentContainer.parse(xml)
		self.assertEquals(container.title, "Fight Club")
		self.assertEquals("EN", container.lang)
		self.assertEquals("FR", container.target_lang)
		self.assertEquals(2, len(container.lines))
		line = container.lines[0]
		self.assertEquals(re.sub("[\t]","",line.content).strip(), "People always ask me\nif I know Tyler Durden")
		self.assertEquals(line.number, "1")
		self.assertEquals(line.start, "00:02:04,738")
		self.assertEquals(line.end, "00:02:07,865")
		self.assertEquals("Les gens me demandent toujours\nsi je connais Tyler Durden", re.sub("[\t]","",line.traduction).strip())
		line = container.lines[1]
		self.assertEquals(re.sub("[\t]","",line.content).strip(), "Three minutes.")
		self.assertEquals(line.number, "2")
		self.assertEquals(line.start, "00:02:08,962")
		self.assertEquals(line.end, "00:02:09,986")
		self.assertEquals("Trois minutes.", re.sub("[\t]","",line.traduction).strip())
